package com.rzico.sbl.ui.report.manager

import androidx.core.view.isVisible
import com.rzico.sbl.R
import com.rzico.sbl.databinding.ActivityReportDriverOrderBinding
import com.rzico.sbl.model.ManagerOrderData
import com.rzico.sbl.ui.report.other.goToOrderDetail
import com.rzico.sbl.viewmodel.ReportDriverViewModel
import com.xinnuo.common.extend.get
import com.xinnuo.common.extend.getExtra
import com.xinnuo.common.extend.gone
import com.xinnuo.common.helper.getTimeString
import com.xinnuo.common.listener.onTabSelectedListener
import com.xinnuo.common.decoration.SpaceTBDecoration
import com.xinnuo.common_ui.base.BaseActivity
import com.xinnuo.common_ui.base.addItems
import com.xinnuo.common_ui.base.count
import com.xinnuo.common_ui.base.loadLinear
import com.xinnuo.slimadapter.SlimAdapter

class ReportDriverOrderActivity : BaseActivity(R.layout.activity_report_driver_order) {

    private val mBinding by lazy { ActivityReportDriverOrderBinding.bind(getRootView()) }

    private var mOrderStarus = ""

    override fun getViewModel() = get<ReportDriverViewModel>()

    override fun initData() {
        initTitle(title = "订单明细")

        initLayout()
        initTabLayout()
    }

    private fun initLayout() {
        mBinding.listLayout.apply {
            emptyLayout.also {
                it.emptyHint.text = "暂无相关订单信息！"
            }

            recycleList.also { view ->
                view.loadLinear(
                    refresh = swipeRefresh,
                    onRefresh = { getData(0) },
                    onLoad = {
                        if (!isLoadingMore) {
                            isLoadingMore = true
                            getData(pageNum)
                        }
                    }
                )

                view.addItemDecoration(SpaceTBDecoration(hasHeader = false))

                mAdapter = SlimAdapter.creator()
                    .register<ManagerOrderData>(R.layout.item_report_driver_order) { jector, bean, _ ->
                        jector.apply {
                            gone(R.id.item_order_img)
                            text(R.id.item_order_title, bean.ename)
                            text(R.id.item_order_type, bean.shippingStatus)
                            text(R.id.item_order_sn, bean.sn.trimStart('8','8','.'))
                            text(R.id.item_order_time, bean.createDate.getTimeString())
                            text(R.id.item_order_driver, bean.name)

                            clicked {
                                goToOrderDetail(
                                    "orderSn" to bean.sn.trimStart('8','8','.')
                                )
                            }
                        }
                    }
                    .attachTo(view)
            }
        }
    }

    /**
     * TabLayout 初始化
     */
    private fun initTabLayout() {
        mBinding
            .orderTab
            .apply {
                onTabSelectedListener {
                    onTabSelected {
                        it?.let {
                            mOrderStarus = when (it.position) {
                                1 -> "unconfirmed"
                                2 -> "dispatch"
                                3 -> "completed"
                                else -> ""
                            }

                            getViewModel().cancelRequest()
                            getViewModel().delay { updateList() }
                        }
                    }
                }

                addTab(this.newTab().setText("全部"), true)
                addTab(this.newTab().setText("待接单"), false)
                addTab(this.newTab().setText("配送中"), false)
                addTab(this.newTab().setText("已完成"), false)
            }
    }

    /**
     * 订单列表
     */
    override fun getData(index: Int, isLoading: Boolean) {
        getViewModel()
            .orderList(
                page = index,
                memberId = intent.getExtra("memberId"),
                status = mOrderStarus,
                beginDate = intent.getExtra("beginDate"),
                endDate = intent.getExtra("endDate"),
                onSuccess = {
                    mList.apply {
                        if (index == 0) {
                            clear()
                            pageNum = 0
                        }
                        addItems(it)
                        if (it.count() > 0) pageNum++
                    }

                    mAdapter.setDataList(mList)
                },
                onFinally = {
                    isLoadingMore = false

                    mBinding
                        .listLayout
                        .apply {
                            swipeRefresh.isRefreshing = false

                            emptyLayout
                                .emptyView
                                .isVisible = mList.isEmpty()
                        }
                }
            )
    }

    /**
     * 刷新列表数据
     */
    private fun updateList() {
        mBinding
            .listLayout
            .apply {
                swipeRefresh.isRefreshing = true

                emptyLayout
                    .emptyView
                    .gone()
            }

        if (mList.isNotEmpty()) {
            mList.clear()
            mAdapter.notifyDataSetChanged()
        }

        getData(0)
    }

}